package com.yunxi.service.dubboService.impl.customer;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.baseDataToHub.pojo.urm.IntentionalInformationVO;
import com.yunxi.baseDataToHub.service.urm.UrmDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.customer.pojo.SatCustomerIntentionVO;
import com.yunxi.customer.service.SatCustomerIntentionDubboService;
import com.yunxi.model.customer.SatCustomerIntention;
import com.yunxi.sat.pojo.SatCustomerClueVO;
import com.yunxi.service.localService.customer.SatCustomerIntentionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 客户意向 服务实现类
 * </p>
 *
 * @author
 * @since 2019-07-11
 */
public class SatCustomerIntentionDubboServiceImpl implements SatCustomerIntentionDubboService {
   private static final Logger logger = LoggerFactory.getLogger(SatCustomerIntentionDubboServiceImpl.class);

   @Autowired
   @Qualifier(value = "satCustomerIntentionServiceImpl")
   SatCustomerIntentionService satCustomerIntentionService;

   @Autowired
   UrmDubboService urmDubboServiceImpl;

   @Override
   public SatCustomerIntentionVO getById(String id) throws Exception {
      SatCustomerIntention satCustomerIntention = satCustomerIntentionService.getById(id);
      if(satCustomerIntention != null){
         SatCustomerIntentionVO satCustomerIntentionVO = new SatCustomerIntentionVO();
         BeanUtils.copyProperties(satCustomerIntention, satCustomerIntentionVO);
         return satCustomerIntentionVO;
      }
      else{
         throw new Exception("操作失败,无法根据id:"+id+"找到对应客户意向!");
      }
   }

   @Override
   public ApiResult saveOrUpdate(SatCustomerIntentionVO satCustomerIntentionVO) throws Exception {
      SatCustomerIntention satCustomerIntention = new SatCustomerIntention();
      BeanUtils.copyProperties(satCustomerIntentionVO,satCustomerIntention);
      satCustomerIntentionService.saveOrUpdate(satCustomerIntention);

      //设置URM客户意向信息字段
      IntentionalInformationVO intentionalInformationVO = setIntentionURM(satCustomerIntentionVO);
      //发送客户意向信息到URM
      return sendIntention2URM(intentionalInformationVO);
   }

   private ApiResult sendIntention2URM(IntentionalInformationVO intentionalInformationVO) throws Exception {
      ApiResult apiResult = null;
      try {
         apiResult = urmDubboServiceImpl.sendIntentionalInformationPMS2Urm(intentionalInformationVO);
      } catch (Exception e) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         e.printStackTrace();
      }
      if (200 != apiResult.getCode()) {
         logger.error("URM接口执行，同步客户意向信息失败，事务回滚!");
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         throw new Exception( "URM接口执行，同步客户意向信息失败");
      }
      if (apiResult.getData()!=null ){
         JSON.parseObject((String) apiResult.getData());
      }
      return apiResult;
   }

   private IntentionalInformationVO setIntentionURM(SatCustomerIntentionVO satCustomerIntentionVO) {
      IntentionalInformationVO intentionalInformationVO = new IntentionalInformationVO();
      intentionalInformationVO.setClueNo(satCustomerIntentionVO.getClueNo());//线索编号
      intentionalInformationVO.setClueType(satCustomerIntentionService.queryDictCn("Customer_ClueType",satCustomerIntentionVO.getClueType()));//线索类型
      intentionalInformationVO.setPartnerCode(satCustomerIntentionVO.getPartnerCode());//渠道编号
      intentionalInformationVO.setCustomerNo(satCustomerIntentionVO.getCustomerNo());//客户ID
      intentionalInformationVO.setCustomerName(satCustomerIntentionVO.getCustomerName());//客户姓名
      intentionalInformationVO.setVehicleCode(satCustomerIntentionVO.getVehicleCode());//15位码
      intentionalInformationVO.setColorInCode(satCustomerIntentionVO.getColorInCode());//内饰颜色代码
      intentionalInformationVO.setColorInName(satCustomerIntentionVO.getColorInName());//内饰颜色名称
      intentionalInformationVO.setColorOutCode(satCustomerIntentionVO.getColorOutCode());//外饰颜色代码
      intentionalInformationVO.setColorOutName(satCustomerIntentionVO.getColorOutName());//外饰颜色名称
      //查询线索
      SatCustomerClueVO satCustomerClueVO = satCustomerIntentionService.getClueInfoByNo(satCustomerIntentionVO.getClueNo());
      if(satCustomerClueVO != null){
         intentionalInformationVO.setTel(satCustomerClueVO.getTel());//手机号
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         if(satCustomerClueVO.getCreateTime() != null){
            String format = simpleDateFormat.format(satCustomerClueVO.getCreateTime());
            intentionalInformationVO.setGetDate(format);//获取时间
         }
         intentionalInformationVO.setIsGet(satCustomerIntentionService.queryDictCn("Clue_GetStatus",satCustomerClueVO.getIsGet().toString()));//线索状态(Clue_GetStatus,0未领取,1已领取,2超时)
         intentionalInformationVO.setGetUser(satCustomerClueVO.getGetUser());//领取人
         intentionalInformationVO.setStatus(satCustomerClueVO.getStatus());//状态1启用0禁用
         intentionalInformationVO.setIsDel(satCustomerClueVO.getIsDel());//是否删除
         intentionalInformationVO.setMemo(satCustomerClueVO.getMemo());//备注
         intentionalInformationVO.setAssignUser(satCustomerClueVO.getAssignUser());//分配人
         intentionalInformationVO.setProvince(satCustomerClueVO.getProvince());//省
         intentionalInformationVO.setCity(satCustomerClueVO.getCity());//市
         intentionalInformationVO.setRegion(satCustomerClueVO.getRegion());//县
         intentionalInformationVO.setIsSelf(satCustomerClueVO.getIsSelf());//是否自己领取
         intentionalInformationVO.setClueSource(satCustomerClueVO.getClueSource());//线索来源
         intentionalInformationVO.setSalAdviserCode(satCustomerClueVO.getSalAdviserCode());//展厅销售顾问
         intentionalInformationVO.setSex(satCustomerClueVO.getSex());//性别
         intentionalInformationVO.setSeries(satCustomerClueVO.getSeries());//车系
         intentionalInformationVO.setIntentModel(satCustomerClueVO.getIntentModel());//车型
         intentionalInformationVO.setGetInfoWay(satCustomerClueVO.getGetInfoWay());//获取信息途径
         intentionalInformationVO.setLevel(satCustomerIntentionService.queryDictCn("Customer_IntentionLevel",satCustomerClueVO.getLevel()));//意向级别
      }
      return intentionalInformationVO;
   }

   @Override
   public void removeById(String id) {
      satCustomerIntentionService.removeById(id);
   }

   @Override
   public PageInfo<SatCustomerIntentionVO> findPageByVO(SatCustomerIntentionVO satCustomerIntentionVO, Integer pageNum, Integer pageSize) throws Exception {
      return satCustomerIntentionService.pageByVo(satCustomerIntentionVO, pageNum, pageSize);
   }

   @Override
   public void removeByListVo(List<SatCustomerIntentionVO> list) throws Exception {
      satCustomerIntentionService.removeByListVo(list);
   }

   @Override
   public void del(List<String> list, String createBy) throws Exception {
      if(list != null){
         for (String id : list){
            SatCustomerIntention byId = satCustomerIntentionService.getById(id);
            if(byId != null){
               byId.setIsDel(1);
               byId.setUpdateBy(createBy);
               satCustomerIntentionService.saveOrUpdate(byId);
            }
            else {
               throw new Exception("操作失败,无法根据id:"+id+"找到对应客户意向!");
            }
         }
      }
      else {
         throw new Exception("操作失败,list为空!");
      }
   }
}
